package spring.cloud.tasks.api.database.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import spring.cloud.tasks.api.database.entity.ZooKeeperClusterDto;
import spring.cloud.tasks.api.database.repository.ZooKeeperClusterDtoRepository;
import spring.cloud.tasks.api.database.service.ZooKeeperClusterDtoService;

import java.util.Date;
import java.util.List;

@Service
public class ZooKeeperClusterDtoServiceImpl implements ZooKeeperClusterDtoService {

    @Autowired
    private ZooKeeperClusterDtoRepository zooKeeperClusterDtoRepository;


    @Transactional
    @Override
    public int insert(String zooKeeperClusterKey, String connectString, String digest, String description, String userId) {
        ZooKeeperClusterDto dto = new ZooKeeperClusterDto();

        //
        dto.setZooKeeperClusterKey(zooKeeperClusterKey);
        dto.setZooKeeperClusterAddressList(connectString);
        dto.setDigest(digest);
        dto.setDescription(description);
        Date now = new Date();

        dto.setCreatedBy(userId);
        dto.setCreateTime(now);

        dto.setLastUpdatedBy(userId);
        dto.setLastUpdateTime(now);
        return zooKeeperClusterDtoRepository.insert(dto);
    }

    @Transactional
    @Override
    public int updateByZooKeeperClusterKey(ZooKeeperClusterDto zooKeeperClusterDto) {
        return zooKeeperClusterDtoRepository.updateByZooKeeperClusterKey(zooKeeperClusterDto);
    }

    @Transactional
    @Override
    public int deleteByZooKeeperClusterKey(String zooKeeperClusterKey) {
        return zooKeeperClusterDtoRepository.deleteByZooKeeperClusterKey(zooKeeperClusterKey);
    }

    @Transactional(readOnly = true)
    @Override
    public ZooKeeperClusterDto selectByZooKeeperClusterKey(String zooKeeperClusterKey) {
        return zooKeeperClusterDtoRepository.selectByZooKeeperClusterKey(zooKeeperClusterKey);
    }

    @Transactional(readOnly = true)
    @Override
    public List<ZooKeeperClusterDto> selectAll() {
        return zooKeeperClusterDtoRepository.selectAll();
    }


}
